﻿namespace TradePublish.SysThread.Grasp
{
    using log4net;
    using TradePublish.Service;
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Threading;
    using TradePublish.Entity;

    public class GraspDissentTMImageSubThread
    {
        private Thread doThread;
        public int id;
        public bool isRunning;
        private ILog log = null;
        public List<TradePublishVo> tradePublishs = new List<TradePublishVo>();

        public GraspDissentTMImageSubThread()
        {
            this.log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        }

        public void start()
        {
            this.isRunning = false;
            this.doThread = new Thread(new ThreadStart(this.threadWork));
            this.doThread.IsBackground = true;
            this.doThread.Start();
        }

        private void threadWork()
        {
            GraspTMManager manager = new GraspTMManager();
            DissentTradeManager manager2 = new DissentTradeManager();
            while (true)
            {
                if (!this.isRunning)
                {
                    Thread.Sleep(0x2710);
                }
                else
                {
                    for (int i = 0; i < this.tradePublishs.Count; i++)
                    {
                        bool hasGrasped = manager2.verifyHasGrasped(this.tradePublishs[i].regNo);
                        if (!hasGrasped)
                        {
                            try
                            {
                                this.log.Info("开始采集注册号=" + this.tradePublishs[i].regNo + "的商标图片...");
                                bool flag2 = manager.doGraspTMImage(this.tradePublishs[i].regNo, this.tradePublishs[i].classNo);
                            }
                            catch (Exception exception)
                            {
                                this.log.Error("保存图片未成功，继续保存图片.", exception);
                                i--;
                                Thread.Sleep(0x2710);
                                continue;
                            }
                        }
                        manager2.setHasGraspImage(this.tradePublishs[i].regNo, hasGrasped);
                        Thread.Sleep(500);
                    }
                    this.isRunning = false;
                    this.log.Info("本次任务执行完毕!");
                }
            }
        }
    }
}

